package com.atguigu.campusintelligentassistantsystem.service;

import com.atguigu.campusintelligentassistantsystem.entity.*;
import com.atguigu.campusintelligentassistantsystem.repository.LeaveApplyRepository;
import com.atguigu.campusintelligentassistantsystem.repository.ScholarshipApplicationRepository;
import com.atguigu.campusintelligentassistantsystem.repository.TeacherLeaveApplyRepository;
import com.atguigu.campusintelligentassistantsystem.repository.TeacherTitleApplyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ApprovalService {

    @Autowired
    private LeaveApplyRepository leaveApplyRepository;

    @Autowired
    private ScholarshipApplicationRepository scholarshipApplicationRepository;

    @Autowired
    private TeacherLeaveApplyRepository teacherLeaveApplyRepository;

    @Autowired
    private TeacherTitleApplyRepository teacherTitleApplyRepository;

    // 学生请假申请提交
    public LeaveApply submitStudentLeave(LeaveApply leaveApply) {
        leaveApply.setStatus(LeaveStatus.PENDING);
        leaveApply.setCreatedAt(LocalDateTime.now());
        leaveApply.setUpdatedAt(LocalDateTime.now());
        return leaveApplyRepository.save(leaveApply);
    }

    // 教师审批学生请假申请
    public LeaveApply approveStudentLeave(Long leaveId, Long teacherId, String decision, String comment) {
        LeaveApply leaveApply = leaveApplyRepository.findById(leaveId).orElse(null);
        if (leaveApply != null) {
            leaveApply.setTeacherId(teacherId);
            // 根据传入的字符串decision设置对应的枚举值
            LeaveStatus status = LeaveStatus.PENDING;
            switch (decision) {
                case "approved":
                    status = LeaveStatus.APPROVED;
                    break;
                case "rejected":
                    status = LeaveStatus.REJECTED;
                    break;
                case "cancelled":
                    status = LeaveStatus.CANCELLED;
                    break;
                default:
                    status = LeaveStatus.PENDING;
                    break;
            }
            leaveApply.setStatus(status);
            leaveApply.setApprovalComment(comment);
            leaveApply.setUpdatedAt(LocalDateTime.now());
            return leaveApplyRepository.save(leaveApply);
        }
        return null;
    }

    // 学生奖学金申请提交
    public ScholarshipApplication submitScholarshipApplication(ScholarshipApplication application) {
        application.setStatus("pending");
        application.setCreatedAt(LocalDateTime.now());
        application.setUpdatedAt(LocalDateTime.now());
        return scholarshipApplicationRepository.save(application);
    }

    // 教师审批学生奖学金申请
    public ScholarshipApplication approveScholarshipByTeacher(Long applicationId, Long teacherId, String comment) {
        ScholarshipApplication application = scholarshipApplicationRepository.findById(applicationId).orElse(null);
        if (application != null) {
            application.setTeacherId(teacherId);
            application.setStatus("reviewing"); // 教师审核通过后等待校长终审
            application.setReviewComment(comment);
            application.setUpdatedAt(LocalDateTime.now());
            return scholarshipApplicationRepository.save(application);
        }
        return null;
    }

    // 校长终审奖学金申请
    public ScholarshipApplication approveScholarshipByPrincipal(Long applicationId, String decision, String comment) {
        ScholarshipApplication application = scholarshipApplicationRepository.findById(applicationId).orElse(null);
        if (application != null) {
            application.setStatus(decision); // approved or rejected
            application.setReviewComment(application.getReviewComment() + "; 校长终审意见: " + comment);
            application.setUpdatedAt(LocalDateTime.now());
            return scholarshipApplicationRepository.save(application);
        }
        return null;
    }

    // 教师请假申请提交
    public TeacherLeaveApply submitTeacherLeave(TeacherLeaveApply leaveApply) {
        leaveApply.setStatus(LeaveStatus.PENDING);
        leaveApply.setCreatedAt(LocalDateTime.now());
        leaveApply.setUpdatedAt(LocalDateTime.now());
        return teacherLeaveApplyRepository.save(leaveApply);
    }

    // 校长审批教师请假申请
    public TeacherLeaveApply approveTeacherLeave(Long leaveId, String decision, String comment) {
        TeacherLeaveApply leaveApply = teacherLeaveApplyRepository.findById(leaveId).orElse(null);
        if (leaveApply != null) {
            // 根据传入的字符串decision设置对应的枚举值
            LeaveStatus status = LeaveStatus.PENDING;
            switch (decision) {
                case "approved":
                    status = LeaveStatus.APPROVED;
                    break;
                case "rejected":
                    status = LeaveStatus.REJECTED;
                    break;
                case "cancelled":
                    status = LeaveStatus.CANCELLED;
                    break;
                default:
                    status = LeaveStatus.PENDING;
                    break;
            }
            
            leaveApply.setStatus(status);
            leaveApply.setApprover("校长");
            leaveApply.setApprovalComment(comment);
            leaveApply.setUpdatedAt(LocalDateTime.now());
            return teacherLeaveApplyRepository.save(leaveApply);
        }
        return null;
    }

    // 教师职称申请提交
    public TeacherTitleApply submitTeacherTitleApply(TeacherTitleApply titleApply) {
        titleApply.setStatus(TeacherTitleStatus.PENDING);
        titleApply.setCreatedAt(LocalDateTime.now());
        titleApply.setUpdatedAt(LocalDateTime.now());
        return teacherTitleApplyRepository.save(titleApply);
    }

    // 校长审批教师职称申请
    public TeacherTitleApply approveTeacherTitleApply(Long applyId, String decision, String comment) {
        TeacherTitleApply titleApply = teacherTitleApplyRepository.findById(applyId).orElse(null);
        if (titleApply != null) {
            // 根据传入的字符串decision设置对应的枚举值
            TeacherTitleStatus status = TeacherTitleStatus.PENDING;
            switch (decision) {
                case "approved":
                    status = TeacherTitleStatus.APPROVED;
                    break;
                case "rejected":
                    status = TeacherTitleStatus.REJECTED;
                    break;
                case "cancelled":
                    status = TeacherTitleStatus.CANCELLED;
                    break;
                default:
                    status = TeacherTitleStatus.PENDING;
                    break;
            }
            
            titleApply.setStatus(status);
            titleApply.setApprover("校长");
            titleApply.setApprovalComment(comment);
            titleApply.setUpdatedAt(LocalDateTime.now());
            return teacherTitleApplyRepository.save(titleApply);
        }
        return null;
    }

    // 获取学生待教师审批的请假申请
    public List<LeaveApply> getPendingStudentLeaves() {
        return leaveApplyRepository.findByStatus(LeaveStatus.PENDING);
    }

    // 获取学生待教师审批的奖学金申请
    public List<ScholarshipApplication> getPendingScholarshipApplications() {
        return scholarshipApplicationRepository.findByStatusOrderByCreatedAtDesc("pending");
    }

    // 获取教师待校长审批的请假申请
    public List<TeacherLeaveApply> getPendingTeacherLeaves() {
        return teacherLeaveApplyRepository.findByStatus(LeaveStatus.PENDING);
    }

    // 获取教师待校长审批的职称申请
    public List<TeacherTitleApply> getPendingTeacherTitleApplications() {
        return teacherTitleApplyRepository.findByStatus(TeacherTitleStatus.PENDING);
    }
    
    // 根据请假类型获取学生请假申请
    public List<LeaveApply> getStudentLeavesByType(LeaveType leaveType) {
        return leaveApplyRepository.findByLeaveType(leaveType);
    }
    
    // 根据请假类型获取教师请假申请
    public List<TeacherLeaveApply> getTeacherLeavesByType(LeaveType leaveType) {
        return teacherLeaveApplyRepository.findByLeaveType(leaveType);
    }
}